package com.esisa.java.swing;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

public class FormAjoutProduit extends JPanel implements ActionListener {
	
	private Form form;
	private JButton add,save,quitter;
	private LabeledTextField labelText;
	
	private GestionClient gCleint;
	public FormAjoutProduit(GestionClient gc) {
		
		gCleint=gc;
		form=new Form(new Produit());
		JPanel p=new JPanel(new FlowLayout(FlowLayout.RIGHT));
		
		labelText=new LabeledTextField("code", 14);
		
		add=new JButton("Ajouter");
		save=new JButton("Enregister");
		quitter=new JButton("Quitter");
		
		add.addActionListener(this);
		save.addActionListener(this);
		quitter.addActionListener(this);
		
		p.add(add);
		p.add(save);
		p.add(quitter);
		
		add(labelText,"North");
		add(form,"Noth");
		add(p,"Weast");
		
	}

	public void actionPerformed(ActionEvent e) {
		JButton b=(JButton)e.getSource();
		switch (b.getText()) {
		case "Ajouter":
			
			Integer code=new Integer(form.getValue(0));
			Integer qte=new Integer(form.getValue(3)) ;
			Double prix=new Double(form.getValue(2));
			Produit p=new Produit(code,form.getValue(1),prix,qte);
			
			int index=gCleint.rechercheClient(labelText.getValue());
			gCleint.setProduit(p,index);
			
			break;
			
		case "Enregister":
			gCleint.save();
			break;
		case "Quitter":
			System.exit(0);
			break;
		default:
			break;
		}
	}

}
